package org.apselex.handlers;

/*
 * Copyright (c) 2008 Evan Williams
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import nu.xom.Document;
import nu.xom.Element;

import org.apache.log4j.Logger;
import org.apselex.ExportJob;
import org.apselex.model.ModelElement;
import org.apselex.providers.DataProvider;
import org.apselex.providers.XmlTranslator;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;

import apselexport.Activator;

/**
 * This class is a handler for the Export Command.
 * 
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class ExportHandler extends AbstractHandler {

	private static Logger logger = Logger.getLogger(ExportHandler.class);

	/**
	 * The constructor.
	 */
	public ExportHandler() {
	}

	/**
	 * execute the Export command.
	 * <p>
	 * this involves -
	 * </p>
	 * <ol>
	 * <li>prompting the user for the catalog file</li>
	 * <li>if the file is specified, open it and refresh the view</li>
	 * </ol>
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil
				.getActiveWorkbenchWindowChecked(event);

		DataProvider dataprovider = Activator.getDefault().getDataProvider();

		try {
			
			// first, produce an XML document which contains the data
			Element root = new Element("root");
			Document doc = new Document(root);

			// add in the tree of tags available
			
			Element ebase = XmlTranslator.transform(dataprovider.getTagTree());
			root.appendChild(ebase);

			// create a job, so that the export can be done asynchronously.
			
			ExportJob job = new ExportJob("Export Tags", root, dataprovider);

			job.setPriority(Job.LONG);
			job.schedule(); // start as soon as possible

			logger.debug("done export now");

		} catch (RuntimeException e) {
			e.printStackTrace();
		}

		// MessageDialog.openInformation(
		// window.getShell(),
		// "PseExport Plug-in",
		// "Hello, Eclipse world");
		return null;
	}
}
